<?php

namespace Form\User;

use App\Dao\Exam\ExamDao;
use App\Dao\Task\TaskHelpDao;
use App\Dao\Task\TaskTypesDao;
use App\Dao\User\UserSpreadSettingDao;
use Form\BaseForm;
use App\Model\Task\TaskHelpCategory;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\PostApi;
use Yj\ColumnBuilder\Column;
use Yj\ColumnBuilder\Table;
use Yj\ColumnBuilder\Templates\Field;
use Yj\ColumnBuilder\Templates\FieldTemplate;
use Yj\ColumnBuilder\Templates\InputTemplate;
use Yj\Form\Elm;


/**
 * @ApiController(prefix="form/setting/friend")
 */
class UserSpreadSetting extends \Yj\Form\BaseForm
{
    protected $yjCode = 'user/UserSpreadSetting';
    const ADD = true;
    const DELETE = true;

    public function getColumn()
    {
        $colum = Column::create();
        $colum->setRules([
            Column::txt()->setLabel('ID')->setProp('id'),
            Column::txt()->setLabel('邀请好友人数')->setProp('people'),
            Column::txt()->setLabel('获取小豆数量')->setProp('num'),
        ]);

        return $colum->toArray();
    }


    /**
     * @PostApi(path="config",description="配置")
     */
    public function config()
    {
        $this->_config();
    }

    /**
     * @PostApi(path="update",description="添加")
     * @FormData(key="id",rule="nullable")
     */
    public function update()
    {
        $id = $this->getParams()->get('id');
        $form = Elm::createForm($this->getRoute($id ? 'edit' : 'add'));
        $form->setRule([
            Elm::input('people', '邀请好友人数')->required(),
            Elm::input('num', '获取小豆数量')->required(),
        ]);
        $id && $form->append(
            Elm::hidden('id', $id)
        );
        $lists = $form->setTitle(($id ? '编辑' : '添加') . '邀请好友配置')->formData(function () use ($id) {
            $formData = [];
            if ($id) {
                $formData = App(UserSpreadSettingDao::class)->where('id', $id)->first();
            }
            return $formData;
        });
        _SUCCESS(formToData($lists));
    }
}